package com.tyk.bookstore.back.order.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.excel.ExcelManager;
import com.tyk.bookstore.back.common.core.model.page.PageVo;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.order.manager.excel.model.OrderExcelVo;
import com.tyk.bookstore.back.order.model.contsant.ExcelConstant;
import com.tyk.bookstore.back.order.model.query.CneeAddressUpdateQuery;
import com.tyk.bookstore.back.order.model.query.OrderDispatchQuery;
import com.tyk.bookstore.back.order.model.query.OrderPageQuery;
import com.tyk.bookstore.back.order.model.vo.OrderCneeAddressVo;
import com.tyk.bookstore.back.order.model.vo.OrderPageVo;
import com.tyk.bookstore.back.order.model.vo.OrderVo;
import com.tyk.bookstore.back.order.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author tyk
 * @description 订单管理
 */
@Tag(name = "订单管理", description = "订单管理")
@RequestMapping({"api/order"})
@RestController
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;
    private final ExcelManager<OrderExcelVo> excelManager;


    @Operation(summary = "查询订单详情", description = "查询订单详情")
    @SaCheckPermission("order:get")
    @GetMapping({"{id}"})
    public R<OrderVo> getById(
            @Schema(description = "订单ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(orderService.getById(id));
    }


    @Operation(summary = "分页查询订单", description = "分页查询订单")
    @SaCheckPermission("order:page")
    @GetMapping({"page"})
    public R<OrderPageVo> getPage(
            @Validated @ParameterObject OrderPageQuery query,
            BindingResult br
    ) {
        return br.hasErrors() ? R.ok(PageVo.instance(OrderPageVo.class)) : R.ok(orderService.getPage(query));
    }


    @Operation(summary = "派单", description = "派单")
    @SaCheckPermission("order:dispatch")
    @PostMapping({"dispatch"})
    public R<Object> dispatch(
            @Validated @RequestBody OrderDispatchQuery query
    ) {
        orderService.dispatch(query);
        return R.ok();
    }


    @Operation(summary = "查询订单收件地址信息", description = "查询订单收件地址信息")
    @SaCheckPermission("order:address:get")
    @GetMapping({"address/{orderId}"})
    public R<OrderCneeAddressVo> getCneeAddressByOrderId(
            @Schema(description = "订单ID") @NotNull @Min(1)
            @PathVariable Long orderId
    ) {
        return R.ok(orderService.getCneeAddressByOrderId(orderId));
    }


    @Operation(summary = "更改订单收货地址", description = "更改订单收货地址")
    @SaCheckPermission("order:address:update")
    @PutMapping("address")
    public R<Object> updateCneeAddress(
            @Validated @RequestBody CneeAddressUpdateQuery query
    ) {
        orderService.updateCneeAddress(query);
        return R.ok();
    }


    @Operation(summary = "删除订单", description = "删除订单")
    @SaCheckPermission("order:address:delete")
    @DeleteMapping({"{id}"})
    public R<Object> deleteById(
            @PathVariable @NotNull @Min(1) Long id
    ) {
        orderService.deleteById(id);
        return R.ok();
    }


    @Operation(summary = "批量删除订单", description = "批量删除订单")
    @SaCheckPermission("order:address:delete")
    @DeleteMapping
    public R<Object> deleteBatchByIdSet(
            @NotNull @NotEmpty Set<Long> idList) {
        orderService.deleteBatchByIdSet(idList);
        return R.ok();
    }


    @Operation(summary = "导出Excel", description = "导出Excel")
    @SaCheckPermission("order:excel:download")
    @GetMapping("excel")
    public void downloadExcel(OrderPageQuery query, HttpServletResponse resp) {
        excelManager.write(
                orderService.getOrderExcelVoList(query),
                ExcelConstant.ORDER_FILE_NAME,
                ExcelConstant.ORDER_SHEET,
                resp
        );
    }

}
